package com.industrial.premu.controller;

import java.time.LocalDate;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;

import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.industrial.common.log.annotation.Log;
import com.industrial.common.log.enums.BusinessType;
import com.industrial.common.security.annotation.RequiresPermissions;
import com.industrial.premu.domain.PreMuEvaluationTimesetting;
import com.industrial.premu.service.IPreMuEvaluationTimesettingService;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.web.page.TableDataInfo;

/**
 * 评价时间设定Controller
 * 
 * @author Zhou_S
 * @date 2024-01-24
 */
@RestController
@RequestMapping("/timesetting")
public class PreMuEvaluationTimesettingController extends BaseController
{
    @Autowired
    private IPreMuEvaluationTimesettingService preMuEvaluationTimesettingService;

    /**
     * 查询评价时间设定列表
     */
    @RequiresPermissions("premu:timesetting:list")
    @GetMapping("/list")
    public TableDataInfo list(PreMuEvaluationTimesetting preMuEvaluationTimesetting)
    {
        String district= SecurityUtils.getLoginUser().getSysUser().getDistrict();
        preMuEvaluationTimesetting.setCounty(district);
        startPage();
        List<PreMuEvaluationTimesetting> list = preMuEvaluationTimesettingService.selectPreMuEvaluationTimesettingList(preMuEvaluationTimesetting);
        return getDataTable(list);
    }

    /**
     * 导出评价时间设定列表
     */
    @RequiresPermissions("premu:timesetting:export")
    @Log(title = "评价时间设定", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PreMuEvaluationTimesetting preMuEvaluationTimesetting)
    {
        String district= SecurityUtils.getLoginUser().getSysUser().getDistrict();
        preMuEvaluationTimesetting.setCounty(district);
        List<PreMuEvaluationTimesetting> list = preMuEvaluationTimesettingService.selectPreMuEvaluationTimesettingList(preMuEvaluationTimesetting);
        ExcelUtil<PreMuEvaluationTimesetting> util = new ExcelUtil<PreMuEvaluationTimesetting>(PreMuEvaluationTimesetting.class);
        util.exportExcel(response, list, "评价时间设定数据");
    }

    /**
     * 获取评价时间设定详细信息
     */
    @RequiresPermissions("premu:timesetting:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(preMuEvaluationTimesettingService.selectPreMuEvaluationTimesettingById(id));
    }

    /**
     * 新增评价时间设定
     */
    @RequiresPermissions("premu:timesetting:add")
    @Log(title = "评价时间设定", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PreMuEvaluationTimesetting preMuEvaluationTimesetting)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();
        if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
            String county=  loginUser.getSysUser().getDistrict();
            preMuEvaluationTimesetting.setCounty(county);
        }

        PreMuEvaluationTimesetting timesetting=   preMuEvaluationTimesettingService.selectPreMuEvaluationTimesetting(preMuEvaluationTimesetting);
        if(timesetting!=null){
            return warn("该区县本年度已完成时间设定！");
        }else{
            return toAjax(preMuEvaluationTimesettingService.insertPreMuEvaluationTimesetting(preMuEvaluationTimesetting));
        }

    }

    /**
     * 修改评价时间设定
     */
    @RequiresPermissions("premu:timesetting:edit")
    @Log(title = "评价时间设定", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PreMuEvaluationTimesetting preMuEvaluationTimesetting)
    {
        return toAjax(preMuEvaluationTimesettingService.updatePreMuEvaluationTimesetting(preMuEvaluationTimesetting));
    }

    /**
     * 删除评价时间设定
     */
    @RequiresPermissions("premu:timesetting:remove")
    @Log(title = "评价时间设定", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(preMuEvaluationTimesettingService.deletePreMuEvaluationTimesettingByIds(ids));
    }
    @GetMapping(value = "/getPreMuEvaluationTimesetting")
    public AjaxResult getPreMuEvaluationTimesetting(PreMuEvaluationTimesetting preMuEvaluationTimesetting)
    {
        LocalDate localDate = LocalDate.now();
        String district= SecurityUtils.getLoginUser().getSysUser().getDistrict();
        preMuEvaluationTimesetting.setCounty(district);
        String years=String.valueOf(localDate.getYear() - 1);
        preMuEvaluationTimesetting.setYears(years);
      PreMuEvaluationTimesetting timesetting=  preMuEvaluationTimesettingService.selectPreMuEvaluationTimesetting(preMuEvaluationTimesetting);
        return success(timesetting);
    }

}
